Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IgnoreUnusedBindingExpression quick-fix #164

Draft
wants to merge 17 commits into
base: net203
Choose a base branch
from

Conversation

reacheight
Copy link
Contributor

No description provided.

@reacheight reacheight changed the title Add IgnoreAndInlineUnusedBindingExpression quick-fix Add IgnoreUnusedBindingExpression quick-fix Aug 11, 2020
{
internal partial class TryWithExpr
{
public override IType Type() => GetPsiModule().GetPredefinedType().Void;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let a: int =
    try 1
    with _ -> 1

{
internal partial class TryFinallyExpr
{
public override IType Type() => GetPsiModule().GetPredefinedType().Void;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let a: int =
    try 1
    finally ()


namespace JetBrains.ReSharper.Plugins.FSharp.Psi.Impl.Tree
{
internal partial class AssertExpr
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please inherit from UnitExpressionBase instead.

@@ -285,7 +301,8 @@ private void DescribeSimpleAlignmentRule((string name, CompositeNodeType nodeTyp
}

private static bool IndentElseExpr(ITreeNode elseExpr, CodeFormattingContext context) =>
elseExpr.GetPreviousMeaningfulSibling().IsFirstOnLine(context.CodeFormatter) && !(elseExpr is IElifExpr);
(elseExpr.GetPreviousMeaningfulSibling().IsFirstOnLine(context.CodeFormatter)
|| !AreAligned(elseExpr, elseExpr.Parent?.FirstChild, context.CodeFormatter)) && !(elseExpr is IElifExpr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move || to the previous line.


override x.IsAvailable _ =
isValid pat && isValid letOrUseExpr && letOrUseExpr.Bindings.Count = 1 && isValid binding.Expression
&& not (binding.Expression :? IDoExpr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move && to the previous line.

let exprToIgnore = getInnermostExpression expr
let ignoredExpr = exprToIgnore.CreateElementFactory().CreateIgnoreApp(exprToIgnore, false)
let replaced = ModificationUtil.ReplaceChild(exprToIgnore, ignoredExpr)
addParensIfNeeded replaced.LeftArgument |> ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be easy to unify it with AddIgnoreFix?

let ignoreInnermostExpression (expr: IFSharpExpression) =
let rec getInnermostExpression (expr: IFSharpExpression) =
match expr with
| :? ISequentialExpr as seqExpr -> getInnermostExpression (seqExpr.Expressions.Last())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExpressionsEnumerable is a bit preferable since it doesn't store the whole collection.

1{caret}
1 |> ignore

()
Copy link
Collaborator

@auduchinok auduchinok Aug 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure it's a correct sequence expression node after the modification, i.e. it should be something like

seqExpr
  1
  1 |> ignore
  ()

and not like

seqExpr
  seqExpr
    1
    1 |> ignore
  ()

It'd be great to have this logic reused in InlineVar refactoring later.

We should probably dump the resulting tree in some of the tests with modifications.

let {caret}c = 1
c + c |> ignore

()
Copy link
Collaborator

@auduchinok auduchinok Aug 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above:

letExpr
  binding
    expr
  seqExpr
    c + c |> ignore

    ()

override x.Text = "Inline and ignore expression"

override x.IsAvailable _ =
isValid pat && isValid letOrUseExpr && letOrUseExpr.Bindings.Count = 1 && isValid binding.Expression
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it'll work on function bindings (and it shouldn't):

do
  let f{caret} _ = ()
  ()

Copy link
Collaborator

@auduchinok auduchinok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small suggestion.


override x.RelativeTestDataPath = "features/quickFixes/ignoreUnusedBindingExpression"

[<Test>] member x.``Function``() = x.DoNamedTest()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try using NotAvailable attribute, as it was pointed in #163 (comment)?

@@ -0,0 +1,8 @@
module Module

let a =
Copy link
Collaborator

@auduchinok auduchinok Aug 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could save up to a half of the dump by using do statement instead of the top-level binding.

module Module

do
  let a =
    1
    1

  ()

A bonus point would be replacing the top-level module with a anon module, but it'd require more changes in test project options, so I'd rather do it separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants